﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;

namespace VO.Business.Utils
{
    public class ImageResizer
    {

        public static void ResizeImage(string sPhysicalPath, ImageFormat oFormat, int width, int heigth)
        {

            try
            {
                System.Drawing.Image oImg = System.Drawing.Image.FromFile(sPhysicalPath);
                System.Drawing.Image oThumbNail = new Bitmap(width, heigth, oImg.PixelFormat);
                Graphics oGraphic = Graphics.FromImage(oThumbNail);
                oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                oGraphic.SmoothingMode = SmoothingMode.AntiAlias;
                oGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                Rectangle oRectangle = new Rectangle(0, 0, width, width);
                oGraphic.DrawImage(oImg, oRectangle);
                oImg.Dispose();
                
                ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
                EncoderParameters encoderParameters;
                encoderParameters = new EncoderParameters(1);
                encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

                oThumbNail.Save(sPhysicalPath, info[1], encoderParameters);
            }
            catch (Exception)
            {
                throw;
            }

        }

    }
}
